#!/bin/sh

# Modified by N.Leiten and adopted for Wive-firmware, renamed in "fs"
# "flash" is a part of midge - mini distribuition for adm5120 based routers.
# Full rewrite for Wive-RTNL (RT3050/RT3052) by Evgeniy Manachkin.
# Copyright (C) 2004-2005 by Vladislav Moskovets <midge at vlad.org.ua>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU GPL v2 or later.

# Maximum rwfs to write
TGZfs_size=196608

# mtd dev for rfs
MTDRWFS=/dev/mtdblock5

# constants
BACKUPCGF=/etc/backup/nvram_backup.dat
RWCONFIGS=/etc/Wireless/rf.bin

get_size() {
  ls -sk "$1" | sed -e "s/ \+/ /g" | cut -d" " -f2
}

usage() {
  echo "Usage: $0 | save | load | restore | backup_nvram | nvramreset | burnrf | fullreset | drop_caches |" >&2
  echo "____________________________________________________________________________" >&2
  echo " " >&2
  echo "save - Save rwfs to mtd." >&2
  echo "load - Load rwfs from mtd." >&2
  echo "restore - Crash RW-FS and reboot for restore clean rwfs." >&2
  echo "backup_nvram - Save nvram snapshot to rwfs in $BACKUPCGF" >&2
  echo "nvramreset - Nvarm clean and load default config." >&2
  echo "burnrf - Burn RF config template to Factory." >&2
  echo "drop_caches - Full drop disck/block devices caches for free memory." >&2
  echo "fullreset - 1. Full zero write in config,factory and rwfs particions." >&2
  echo "	    2. Reboot device." >&2
  echo "	    3. Write temlate for RF config (Factory) to flash." >&2
  echo "	    4. Load and write default setting (Config) to flash." >&2
  echo "	    5. Generate new macs, keys and others." >&2
  echo "	    6. Generate new mac and flash it." >&2
  echo "____________________________________________________________________________" >&2
 exit 1
}

load() {
  local dst=/ 
  echo "Loading TGZfs"
  bzcat $MTDRWFS | tar xf - -C $dst>>/dev/null
}

save() {
  echo "Save curent date and current time to rwfs"
  date +%Y%m%d%H%M > /etc/compile-date
  echo "Compress config files"
  tmp="/tmp/tgzfs"
  tar cf - /etc | bzip2 -9 > $tmp
  if [ "$(get_size $tmp)" -lt $(($TGZfs_size/1024)) ]; then
    echo "Write RW-FS to flash"
    mtd_write write $tmp RW-FS
    echo "Config saved. OK."
  else
    echo "Error: File $tmp too big: $(get_size $tmp)k"
  fi
  rm -f $tmp
}

restore() {
    echo "Clear rwfs and reboot. Please go smoke..."
    mtd_write erase RW-FS
    reboot
}

nvramreset() {
    echo "Load user defaults."
    nvram_default 2860
}

burnrf() {
    echo "Erase wifi Factory part and write defaults"
    mtd_write erase Factory
    mtd_write write $RWCONFIGS Factory
}

fullreset() {
    echo "Stop services and unload modules..."
    killall -q goahead
    killall -q -SIGKILL goahead
    wifi_unload.sh
    echo "Erase RW-FS partitions"
    mtd_write erase RW-FS
    burnrf
    echo "Clear Config partitions."
    nvram_clear 2860
    reboot
}

backup_nvram() {
    mkdir -p /etc/backup
    echo "Store settings to backup file...."
    echo "#The following line must not be removed." > "$BACKUPCGF"
    echo "Default" >> "$BACKUPCGF"
    ralink_init show 2860 >> "$BACKUPCGF"
}

load_nvram() {
    echo "Load settings from backup file...."
    ralink_init renew 2860 "$BACKUPCGF"
}

# free memory
drop_caches() {
    for i in `seq 1 3`; do
        echo $i > /proc/sys/vm/drop_caches
	sync
    done
    echo 0 > /proc/sys/vm/drop_caches
}

main() {
  local cmd="$1"
  [ -z "$cmd" ] && usage
  shift
  case "$cmd" in 
    load) 
      load $@ 
      ;;
    save) 
      save $@ 
      ;;
    restore)
      restore $@
      ;;
    nvramreset)
      nvramreset $@
      ;;
    burnrf)
      burnrf $@
      ;;
    fullreset)
      fullreset $@
      ;;
    backup_nvram)
      backup_nvram $@
      ;;
    load_nvram)
      load_nvram $@
      ;;
    drop_caches)
      drop_caches $@
      ;;
    *) 
      usage $@ 
      ;;
  esac
}

main $@
